// Package requests 处理请求数据和表单验证
package requests

import (
	_ "zpay/app/requests/validators"

	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
)

// CreateOrderRequest 创建订单请求信息
type CreateOrderRequest struct {
	OrderName string  `json:"order_name,omitempty" valid:"order_name"`
	Amount    float64 `json:"amount,omitempty" valid:"amount"`
	Source    string  `json:"source,omitempty" valid:"source"`
}

// ValidateCreateOrderRequest 验证创建订单请求信息
func ValidateCreateOrderRequest(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"order_name": []string{"required", "max_cn:100"},
		"amount":     []string{"required", "min_val:0.01", "max_val:10000"},
		"source":     []string{"required", "alpha_num", "between:1,20"},
	}

	messages := govalidator.MapData{
		"order_name": []string{
			"required:订单名称 OrderName 是必填项",
			"max_cn:订单名称最大长度为100",
		},
		"amount": []string{
			"required:amount 字段必填",
			"min_val:amount 不能小于0.01",
			"max_val:amount 不能大于10000",
		},
		"source": []string{
			"required:source 字段必填",
			"alpha_num:source字段只能为字母或数字",
			"between:source字段长度需在 1~20 之间",
		},
	}

	errs := validate(data, rules, messages)
	return errs
}

// StatusOrderRequest 查询订单请求信息
type StatusOrderRequest struct {
	OrderNo string `form:"order_no" valid:"order_no"`
}

// ValidateStatusOrderRequest 验证查询订单信息
func ValidateStatusOrderRequest(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"order_no": []string{"required", "digits:23"},
	}

	messages := govalidator.MapData{
		"order_no": []string{
			"required:订单号 OrderNo 是必填项",
			"digits:订单号必须为 23 个数字",
		},
	}

	errs := validate(data, rules, messages)
	return errs
}
